package com.example.shortmessage.dao;

import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.example.shortmessage.data.entity.MessageData;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

/**
 * 短信类
 * @author 27638
 */
public class ObtainShortMessage {
    private static Uri SMS_INBOX = Uri.parse("content://sms/");

    /**
     * 获取信息数据
     * @param context 上下文
     */
    public static void obtainShortMessage(Context context){
        List<MessageData> messageDataList = new ArrayList<>();
        String TAG = ObtainShortMessage.class.getSimpleName();
        ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(SMS_INBOX, null, null, null, "date desc");
        if (null == cur) {
            Log.i("ooc", "************cur == null");
            return;
        }

        while (cur.moveToNext()) {
            String _id = cur.getString(cur.getColumnIndex("_id"));
            //手机号
            String number = cur.getString(cur.getColumnIndex("address"));
            //联系人姓名列表
            String name = cur.getString(cur.getColumnIndex("person"));
            //短信内容
            String body = cur.getString(cur.getColumnIndex("body"));
            //是否阅读0未读，1已读
            int read = cur.getInt(cur.getColumnIndex("read"));
            //短信类型1是接收到的，2是已发出
            int type = cur.getInt(cur.getColumnIndex("type"));
            //时间
            long date = cur.getLong(cur.getColumnIndex("date"));
            Log.d(TAG, "obtainShortMessage: "+number);
            MessageData data = new MessageData(Integer.parseInt(_id), number, name, body, read, type, date);
            messageDataList.add(data);
        }
        LitePal.saveAll(messageDataList);

    }

    /**
     * 按ID删除信息
     * @param context 上下文
     * @param _id ID
     */
    public static void delete_id(Context context,String _id){
        ContentResolver cr = context.getContentResolver();
        cr.delete(SMS_INBOX,"_id=?", new String[] {_id});

    }



    /**
     * 按电话删除信息
     * @param context 上下文
     * @param phone ID
     */
    public static void delete_phone(Context context,String phone){
        ContentResolver cr = context.getContentResolver();
        cr.delete(SMS_INBOX,"_id=?", new String[] {phone});
    }

    /**
     * 发送信息
     * @param phoneNumber
     * @param message
     */
    public static void send(Context context,String phoneNumber, String message){
        //获取短信管理器
        android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
        //拆分短信内容（手机短信长度限制）,貌似长度限制为140个字符,就是
        //只能发送70个汉字,多了要拆分成多条短信发送
        //第四五个参数,如果没有需要监听发送状态与接收状态的话可以写null
        List<String> divideContents = smsManager.divideMessage(message);
        for (String text : divideContents) {
            Intent itSend = new Intent();
            itSend.setAction("com.example.shortmessage.receiver.StatusReceiver");
            itSend.setPackage(context.getPackageName());
            PendingIntent mSendPI = PendingIntent.getBroadcast(context, 0, itSend, 0);
            smsManager.sendTextMessage(phoneNumber, null, text, mSendPI, null);
        }
    }
}
